﻿package com.schimanek.animation  
{
	import flash.events.EventDispatcher;
	
	/**
	* ...
	* @author DefaultUser (Tools -> Custom Arguments...)
	*/
	import com.schimanek.animation.PropertySwinger
	public class SwingListItem extends EventDispatcher
	{
		public var oToSwing				:Object;
		public var hasReachedAllGoals	:Boolean = false;
		
		private var _propSwingList		:Array = new Array();		
		private var _oGoal				:Object;
		
		private var _power				:Number;
		private var _damping			:Number;
		
		
		public function SwingListItem(oToSwing:Object, aPropsToSwing:Array, oGoal:Object, aGoalProps:Array, aOffsets:Array, power:Number = .5, damping:Number = .5):void		
		{
			this.oToSwing = oToSwing;
			_oGoal = oGoal;
			
			_power = power;
			_damping = damping;
			//+_properties);
			var pName:String;
			var offsets:Array = aOffsets;
			for (var p:uint = 0; p < aPropsToSwing.length; ++p)			{
				trace("p: "+p);
				if (offsets[p] == null) {
					offsets[p] = 0;
				}
				
				var pSwing:PropertySwinger = new PropertySwinger(oToSwing, aPropsToSwing[p], oGoal, aGoalProps[p],aOffsets[p]);
				_propSwingList.push(pSwing);
				
			}			
		}	

		//public function updateSwingParams(
		public function update():void
		{
			var j:uint = _propSwingList.length;			
			//trace(pNameList);
			hasReachedAllGoals = true;
			while (--j > -1)
			{				
				(_propSwingList[j] as PropertySwinger).swing(_power, _damping);
				//trace("prop: "+_object[pNameList[j-1]]);
				if (!(_propSwingList[j] as PropertySwinger).hasReachedGoal) {
					hasReachedAllGoals = false;
				}				
				//j--
			}
			if (hasReachedAllGoals) {
				dispatchEvent(new SwingerEvent(SwingerEvent.REACHED_ALL_GOALS));
			}else{
				dispatchEvent(new SwingerEvent(SwingerEvent.UPDATE));
			}
		}
	}
	
}